package kimi

import (
	"ai-bot/args"
	bytes2 "bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

type ReqBody struct {
	Model       string     `json:"model"`
	Messages    []Messages `json:"messages"`
	Temperature float64    `json:"temperature"`
}
type Messages struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}

type RespBody struct {
	ID      string    `json:"id"`
	Object  string    `json:"object"`
	Created int       `json:"created"`
	Model   string    `json:"model"`
	Choices []Choices `json:"choices"`
	Usage   Usage     `json:"usage"`
}
type Message struct {
	Role    string `json:"role"`
	Content string `json:"content"`
}
type Choices struct {
	Index        int     `json:"index"`
	Message      Message `json:"message"`
	FinishReason string  `json:"finish_reason"`
}
type Usage struct {
	PromptTokens     int `json:"prompt_tokens"`
	CompletionTokens int `json:"completion_tokens"`
	TotalTokens      int `json:"total_tokens"`
}

//var url string = "https://api.moonshot.cn/v1/chat/completions"
//var method string = "POST"
//var apiKey string = "sk-CAlHGgzpCzUoQRzVMpF22YI4buXjayjyaAwhESSItitMJk0Y"
//var model string = "moonshot-v1-8k"
//var temperature = 0.3

/*
*

req:

	{
	    "model": "moonshot-v1-8k",
	    "messages": [
	        {
	            "role": "system",
	            "content": "你是 Kimi，由 Moonshot AI 提供的人工智能助手，你更擅长中文和英文的对话。你会为用户提供安全，有帮助，准确的回答。同时，你会拒绝一切涉及恐怖主义，种族歧视，黄色暴力等问题的回答。Moonshot AI 为专有名词，不可翻译成其他语言。"
	        },
	        {
	            "role": "user",
	            "content": "你好，我叫李雷，1+1等于多少？"
	        }
	    ],
	    "temperature": 0.3
	}

resp:

	{
	    "id": "cmpl-526f4e43c38b46c1a41b62b32e537e33",
	    "object": "chat.completion",
	    "created": 1355344,
	    "model": "moonshot-v1-8k",
	    "choices": [
	        {
	            "index": 0,
	            "message": {
	                "role": "assistant",
	                "content": "你好，李雷！1+1等于2。这是一个基本的数学加法运算。如果你有任何其他问题，欢迎随时问我。"
	            },
	            "finish_reason": "stop"
	        }
	    ],
	    "usage": {
	        "prompt_tokens": 87,
	        "completion_tokens": 29,
	        "total_tokens": 116
	    }
	}
*/
/**
调用 kimi 接口
*/
func SendToKiMi(msg string) string {
	defer func() {
		if err := recover(); err != nil {
			fmt.Println(fmt.Sprintf("发生了错误。。。 %v", err))
		}
	}()
	fmt.Println("send to kimi start......")
	reqBody := ReqBody{
		Model: args.KiMi.Model,
		Messages: []Messages{
			Messages{
				Role:    "user",
				Content: msg,
			},
		},
		Temperature: args.KiMi.Temperature,
	}
	bytes, e := json.Marshal(&reqBody)
	if e != nil {
		fmt.Println("err..... send kimi...")
		return ""
	}
	client := &http.Client{}
	req, err := http.NewRequest(args.KiMi.Method, args.KiMi.Url, bytes2.NewBuffer(bytes))

	if err != nil {
		fmt.Println(err)
		return ""
	}
	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Authorization", "Bearer "+args.KiMi.ApiKey)

	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return ""
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return ""
	}
	respBody := RespBody{}
	json.Unmarshal(body, &respBody)
	//fmt.Println(string(body))
	fmt.Println(respBody)

	fmt.Println("send to kimi end......")
	return respBody.Choices[0].Message.Content
}

// http://127.0.0.1:7600/wcf/send_txt
// http://localhost:7600/wcf/send_txt
